package com.ycsj.goldmedalnewconcept;

import io.rong.imkit.RongIM;

import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.Platform.ShareParams;
import cn.sharesdk.onekeyshare.OnekeyShare;
import cn.sharesdk.onekeyshare.ShareContentCustomizeCallback;
import cn.sharesdk.sina.weibo.SinaWeibo;

import com.mob.MobSDK;
import com.ycsj.goldmedalnewconcept.R;
import com.ycsj.goldmedalnewconcept.activity.GrammarThirdActivity;
import com.ycsj.goldmedalnewconcept.activity.SendRedPackageActivity;
import com.ycsj.goldmedalnewconcept.activity.ShareActivity;
import com.ycsj.goldmedalnewconcept.activity.UserInfoActivity;
import com.ycsj.goldmedalnewconcept.constant.ConfigConstant;
import com.ycsj.goldmedalnewconcept.constant.Constant;
import com.ycsj.goldmedalnewconcept.utils.SPUtil;
import com.ycsj.goldmedalnewconcept.utils.SPUtils;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebChromeClient.CustomViewCallback;
import android.webkit.WebSettings;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
import android.widget.Toast;

/**
 * 使用WebView播放视频时需要注意的地方： 1、加网络访问权限（及其他所需要的权限）；
 * 2、WebViewClient中方法shouldOverrideUrlLoading可用来实现点击webView页面的链接；
 * 3、WebView中播放视频需要添加webView.setWebChromeClient(new WebChromeClient())；
 * 4、视频竖屏时，点击全屏，想要切换到横屏全屏的状态，那么必须在Manifest.xml配置文件该Activity的
 * 配置文件中添加android:configChanges="orientation|screenSize"语句。
 * 5、如果视频不能播放，或者播放比较卡，可以采用硬件加速，即在Application，或所在的Activity的配置文件中添加
 * android:hardwareAccelerated="true"即可。
 * 
 */
public class scoreReportActivity extends Activity {
	private WebView webView;
	private View xCustomView;
	private ProgressDialog waitdialog = null;
	private CustomViewCallback xCustomViewCallback;
	private myWebChromeClient xwebchromeclient;
	private String[] split;
	private String[] times;
	private String[] marks;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
//		requestWindowFeature(Window.FEATURE_NO_TITLE);// 去掉应用标题
//		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
//				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.activity_tk);

		item = new HashMap<String, Class<?>>();
		// item.put("register", registerActivity.class);
		// item.put("retrieve-password", retrievepasswordActivity.class);
		// item.put("set-password", setpasswordActivity.class);
		// item.put("student-login", studentloginActivity.class);
		// item.put("student-tryout", studenttryoutActivity.class);
		// item.put("teacher-login", teacherloginActivity.class);
		// item.put("teacher-me", teachermeActivity.class);
		// item.put("teacher-me-bjsz", teachermebjszActivity.class);
		// item.put("teacher-me-grxx", teachermegrxxActivity.class);
		// item.put("teacher-me-gywm", teachermegywmActivity.class);
		item.put("question", questionActivity.class);
		item.put("student-practice", studentpracticeActivity.class);
		item.put("student-task", studenttaskActivity.class);
		item.put("student-test", studenttestActivity.class);
		item.put("student-record", studentrecordActivity.class);
		item.put("teacher-record", teacherrecordActivity.class);
		item.put("teacher-task", TkActivity.class);
		item.put("teacher-task-busyworkExam",
				teachertaskbusyworkExamActivity.class);
		item.put("teacher-task-busyworkExam-screen",
				teachertaskbusyworkExamscreenActivity.class);
		item.put("teacher-task-busyworkExam-sDetail",
				teachertaskbusyworkExamsDetailActivity.class);
		item.put("teacher-task-busyworkExam-sList",
				teachertaskbusyworkExamsListActivity.class);
		item.put("searchPeoper", searchPeoperActivity.class);
		item.put("score-report", scoreReportActivity.class);
		item.put("analysis", analysisActivity.class);

		waitdialog = new ProgressDialog(this);
		waitdialog.setTitle("提示");
		waitdialog.setMessage("页面加载中...");
		waitdialog.setIndeterminate(true);
		waitdialog.setCancelable(true);
		waitdialog.show();

		webView = (WebView) findViewById(R.id.webView);

		WebSettings ws = webView.getSettings();
		ws.setBuiltInZoomControls(true);// 隐藏缩放按钮
		ws.setLayoutAlgorithm(LayoutAlgorithm.NORMAL);// 排版适应屏幕

		ws.setUseWideViewPort(true);// 可任意比例缩放
		ws.setLoadWithOverviewMode(true);// setUseWideViewPort方法设置webview推荐使用的窗口。setLoadWithOverviewMode方法是设置webview加载的页面的模式。

		ws.setSavePassword(true);
		ws.setSaveFormData(true);// 保存表单数据
		ws.setJavaScriptEnabled(true);
		ws.setGeolocationEnabled(true);// 启用地理定位
		ws.setGeolocationDatabasePath("/data/data/org.itri.html5webview/databases/");// 设置定位的数据库路径
		ws.setDomStorageEnabled(true);

		String appCachePath = getApplicationContext().getCacheDir()
				.getAbsolutePath();
		ws.setAppCachePath(appCachePath);
		ws.setAllowFileAccess(true);
		ws.setAppCacheEnabled(true);
		ws.setJavaScriptCanOpenWindowsAutomatically(true);
		ws.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN); // 支持内容重新布局

		ws.supportMultipleWindows(); // 多窗口

		ws.setSupportMultipleWindows(true);// 新加
		ws.setLoadsImagesAutomatically(true);

		xwebchromeclient = new myWebChromeClient();
		webView.setWebChromeClient(xwebchromeclient);
		webView.setWebViewClient(new myWebViewClient(

		));
		// webView.loadUrl("http://v.qq.com/iframe/player.html?vid=i018531ty8g&tiny=0&auto=0");
		webView.addJavascriptInterface(new WebAppInterface(this), "Android");
		webView.loadUrl(Constant.SERVER_tk + "/score-report.html");
		// webView.loadUrl("http://m.moregolden.com:36356/score-report.html");
		// webView.loadUrl("http://139.129.213.18:36356/score-report.html");

	}

	public class myWebViewClient extends WebViewClient {
		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			view.loadUrl(url);
			return false;
		}

		@Override
		public void onPageFinished(WebView view, String url) {
			super.onPageFinished(view, url);
			waitdialog.dismiss();

		}
	}

	public class myWebChromeClient extends WebChromeClient {
		private View xprogressvideo;

		// 播放网络视频时全屏会被调用的方法
		@Override
		public void onShowCustomView(View view, CustomViewCallback callback) {

			Toast.makeText(scoreReportActivity.this, "onShowCustomView", 0)
					.show();

			setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
			webView.setVisibility(View.INVISIBLE);
			// 如果一个视图已经存在，那么立刻终止并新建一个
			if (xCustomView != null) {
				callback.onCustomViewHidden();
				return;
			}
			xCustomView = view;
			xCustomViewCallback = callback;
		}

		// 视频播放退出全屏会被调用的
		@Override
		public void onHideCustomView() {
			if (xCustomView == null)// 不是全屏播放状态
				return;

			setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
			xCustomView.setVisibility(View.GONE);
			xCustomView = null;
			xCustomViewCallback.onCustomViewHidden();
			webView.setVisibility(View.VISIBLE);
		}

		// 视频加载时进程loading
		@Override
		public View getVideoLoadingProgressView() {
			if (xprogressvideo == null) {
				LayoutInflater inflater = LayoutInflater
						.from(scoreReportActivity.this);
				xprogressvideo = inflater.inflate(
						R.layout.video_loading_progress, null);
			}
			return xprogressvideo;
		}
	}

	/**
	 * 判断是否是全屏
	 * 
	 * @return
	 */
	public boolean inCustomView() {
		return (xCustomView != null);
	}

	/**
	 * 全屏时按返加键执行退出全屏方法
	 */
	public void hideCustomView() {
		xwebchromeclient.onHideCustomView();
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
	}

	@Override
	protected void onResume() {
		super.onResume();
		super.onResume();
		webView.onResume();
		webView.resumeTimers();

		/**
		 * 设置为横屏
		 */
		if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
			setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		}
	}

	@Override
	protected void onPause() {
		super.onPause();
		webView.onPause();
		webView.pauseTimers();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		super.onDestroy();
		webView.loadUrl("about:blank");
		webView.stopLoading();
		webView.setWebChromeClient(null);
		webView.setWebViewClient(null);
		webView.destroy();
		webView = null;
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (inCustomView()) {
				// webViewDetails.loadUrl("about:blank");
				hideCustomView();
				return true;
			} else {
				webView.loadUrl("about:blank");
				scoreReportActivity.this.finish();
			}
		}
		return false;
	}

	private HashMap<String, Class<?>> item;

	public class WebAppInterface {
		Context mContext;

		/** Instantiate the interface and set the context */
		WebAppInterface(Context c) {
			mContext = c;
		}

		@JavascriptInterface
		public void showToast(String toast) {
			// Toast.makeText(scoreReportActivity.this, ""+toast, 0).show();

			Intent intent2 = new Intent(scoreReportActivity.this,
					item.get(toast));
			startActivity(intent2);
		}

		@JavascriptInterface
		public void finishActivity() {
			finish();
		}

		@JavascriptInterface
		public void IntentButtonActivity() {
			Log.e("IntentButtonActivity", "run");

			Intent intent = new Intent(mContext, ButtonActivity.class);
			startActivity(intent);
			finish();
		}

		@JavascriptInterface
		public void ExitLogin() {
			Log.e("ExitLogin", "ExitLogin");
			SPUtils.put(mContext, ConfigConstant.IS_LOGIN_ON, false);
			SPUtils.put(mContext, ConfigConstant.USERTOKEN, "");
			SPUtils.put(mContext, ConfigConstant.USERNAME, "");
			SPUtils.put(mContext, "Friendlistjson", "");
			SPUtils.put(mContext, "haveHomeWork", "0");
			SPUtil.putString(getApplicationContext(), "account", "");
			SPUtil.putString(getApplicationContext(), "token1", "");
			SPUtil.putString(getApplicationContext(), "token2", "");
			SPUtil.putString(getApplicationContext(), "name", "");
			SPUtil.putString(getApplicationContext(), "icon", "");
			SPUtil.putString(getApplicationContext(), "role", "");

			Intent intent5 = new Intent(mContext, StartActivity.class);
			startActivity(intent5);
			RongIM.getInstance().logout();
			finish();
		}

		@JavascriptInterface
		public void shareResults(String scoreInfo) {
			//
			// //moduleType=1&userType=0&time=41&mark=0&num=18271261779
			
			Intent intent = new Intent(mContext, ShareActivity.class);
			intent.putExtra("scoreInfo", scoreInfo);
			startActivity(intent);
		}
	}
}
